/*
// MASK css
.loadmask { z-index: 999; position: absolute; top:0; left:0; opacity: 0.7; background-color: #000; width: 100%; height: 100%; }
.loadmask-msg { z-index: 20001; position: absolute; padding:10px; margin: 0 auto; display: inline-block; border: 1px solid #ccc; border-color: rgba(255,255,255,0.5); background: #333; background: rgba(0,0,0,0.5); -moz-border-radius: 10px; -webkit-border-radius: 10px; border-radius: 10px; -webkit-background-clip: padding-box; -moz-box-shadow: 0 2px 4px 4px rgba(0,0,0,0.2); -webkit-box-shadow: 0 2px 4px 4px rgba(0,0,0,0.2); box-shadow: 0 2px 4px 4px rgba(0,0,0,0.2);}
.loadmask-msg div { padding: 15px 10px 15px 60px; background: url('../img/loading.gif') no-repeat 0 0; color:#fff; cursor:wait; font: normal 16px Tahoma, helvetica, sans-serif;  text-shadow: 1px 1px 1px #000; -moz-border-radius: 10px; -webkit-border-radius: 10px; border-radius: 10px;}

.masked { overflow: hidden !important; }
.masked-relative { position: relative !important; }
.masked-hidden { visibility: hidden !important; }
*/

;(function($){
	/**
	 * Shows loading mask over selected elements
	 *
	 * @param string label		Text message to display over the mask. If not provided only mask will be displayed  	
	 * @param int delay			A number of milliseconds to delay showing the mask (if action is completed before that - mask won't show up)
	 * @param int fadein		if exists - add fadeIn animation to the mask
	 */
	$.fn.mask = function(label, delay, fadein){
		$(this).each(function(){
			var element = $(this);
			if (!delay) delay=0;
			element.data("_mask_timeout", setTimeout(function(){ $.maskElement(element, label, fadein); }, delay));
		});
		return $(this);
	};
	/**
	 * Removes loading mask
	 */
	$.fn.unmask = function(speed, callback){ $(this).each(function(){ $.unmaskElement($(this), speed, callback); }); return this; };
	$.maskElement = function(element, label, fadein){
		if (!element.is(':visible')) return;
		//if this element has delayed mask scheduled then remove it and display the new one
		if (element.data("_mask_timeout") !== undefined){ clearTimeout(element.data("_mask_timeout")); element.removeData("_mask_timeout"); }
		//if(element.hasClass("masked")) $.unmaskElement(element);
		if(!element.hasClass("masked")){
			if(element.css("position") == "static") element.addClass("masked-relative");
			element.addClass("masked");
			var maskDiv = $('<div class="loadmask"></div>'), L = element[0].offsetLeft, T = element[0].offsetTop, W = element[0].offsetWidth, H = element[0].offsetHeight, zIdx = parseInt(element.css('z-index'))||500;
			maskDiv.css({ top: T, left: L, height: H, width: W, zIndex: zIdx+1 });
			//auto height fix for IE
			if(navigator.userAgent.toLowerCase().indexOf("msie") > -1) maskDiv.height(element.height() + parseInt(element.css("padding-top")) + parseInt(element.css("padding-bottom"))).width(element.width() + parseInt(element.css("padding-left")) + parseInt(element.css("padding-right")));
			element.parent().append(maskDiv);
			if (fadein) maskDiv.fadeTo(0,0).stop().dequeue().fadeTo(fadein,.4);
			if(label){
				var maskMsgDiv = $('<div class="loadmask-msg" style="display:none;"></div>');
				maskMsgDiv.append('<div>' + label + '</div>');
				element.parent().append(maskMsgDiv);
				maskMsgDiv.css({ top: T, left: L, marginTop: (H-maskMsgDiv.height())/2, marginLeft: (W-maskMsgDiv.width())/2 });	// center
				maskMsgDiv.show();
			}
		}
		else if(label){
			var maskMsgDiv=element.parent().find('.loadmask-msg'), msg=maskMsgDiv.find('div'), L=element[0].offsetLeft, T=element[0].offsetTop, W=element[0].offsetWidth, H=element[0].offsetHeight;
			if (msg.length) msg.html(label);
			else {
				maskMsgDiv = $('<div class="loadmask-msg" style="display:none;"></div>');
				maskMsgDiv.append('<div>' + label + '</div>');
				element.parent().append(maskMsgDiv);
			}
			maskMsgDiv.css({ top: T, left: L, marginTop: (H-maskMsgDiv.height())/2, marginLeft: (W-maskMsgDiv.width())/2 });	// center
			maskMsgDiv.show();
		}	
	};
	$.unmaskElement = function(element, speed, callback){ 
		if (element.data("_mask_timeout") !== undefined) { clearTimeout(element.data("_mask_timeout")); element.removeData("_mask_timeout"); }	//if this element has delayed mask scheduled then remove it
		element.parent().find(".loadmask-msg,.loadmask").stop().dequeue().fadeOut(speed || 500, function(){ 
			$(this).remove();
			if (callback && typeof callback === 'function') callback.call(this);
		});
		element.removeClass("masked");
		element.removeClass("masked-relative");
		element.find("select").removeClass("masked-hidden");
	};
})(jQuery);
